package me.zhanshi123.enchantfix;

import com.google.common.base.Charsets;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class EnchantNameMapper {

    private static FileConfiguration config = new YamlConfiguration();

    public static void writeFile(InputStream input, File file) {
        try {
            int index;
            byte[] bytes = new byte[10240];
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            while ((index = input.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, index);
                fileOutputStream.flush();
            }
            fileOutputStream.close();
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void update() {
        FileConfiguration builtIn = new YamlConfiguration();
        try {
            InputStream inputStream = EnchantFix.getInstance().getResource(getFileName(EnchantFix.getConfigManager().getLanguage()));
            if (inputStream == null) {
                inputStream = EnchantFix.getInstance().getResource(getFileName("en"));
            }
            builtIn.load(new InputStreamReader(inputStream, Charsets.UTF_8));
            long[] count = {0};
            builtIn.getKeys(true).stream()
                    .filter(key -> config.get(key) == null)
                    .forEach(key -> {
                        config.set(key, builtIn.get(key));
                        count[0]++;
                    });
            if (count[0] != 0) {
                config.save(f);
            }
        } catch (IOException | InvalidConfigurationException e) {
            e.printStackTrace();
        }
    }

    private static String getFileName(String lang) {
        return lang + ".yml";
    }

    public static void init() {
        try {
            File folder = new File(EnchantFix.getInstance().getDataFolder(), "messages");
            folder.mkdirs();
            f = new File(EnchantFix.getInstance().getDataFolder().getAbsolutePath() + File.separator + "messages" + File.separator + EnchantFix.getConfigManager().getLanguage() + ".yml");
            if (!f.exists()) {
                InputStream inputStream = EnchantFix.getInstance().getResource(getFileName(EnchantFix.getConfigManager().getLanguage()));
                if (inputStream == null) {
                    inputStream = EnchantFix.getInstance().getResource(getFileName("en"));
                    f = new File(EnchantFix.getInstance().getDataFolder().getAbsolutePath() + File.separator + "messages" + File.separator + EnchantFix.getConfigManager().getLanguage() + ".yml");
                }
                writeFile(inputStream, f);
            }
            config.load(new InputStreamReader(new FileInputStream(f), StandardCharsets.UTF_8));
        } catch (IOException | InvalidConfigurationException e) {
            e.printStackTrace();
        }
        update();
    }

    private static File f;

    public static String getEnchantName(Enchantment enchantment) {
        return config.getString(enchantment.getName(),enchantment.getName());
    }
}
